package com.abbas.abchat.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.abbas.abchat.AbChatApplication;
import com.abbas.abchat.R;
import com.abbas.abchat.adapter.FriendRoundAdapter;
import com.abbas.abchat.dao.ChatTableDao;
import com.abbas.abchat.dao.FriendRoundDao;
import com.abbas.abchat.dao.impl.AccountDaoImpl;
import com.abbas.abchat.dao.impl.ChatTableDaoImpl;
import com.abbas.abchat.dao.impl.FriendRoundImpl;
import com.abbas.abchat.entity.FriendRound;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

import java.util.List;

public class FriendRoundActivity extends BaseActivity implements View.OnClickListener{

    private ImageView imBack;
    private ImageView im;
    private ListView lv;
    private ImageView back;
    private ImageView add;
    private TextView name;


    private ChatTableDao chatTableDao;
    private List<Integer> allOfId;

    private FriendRoundDao friendRoundDao;
    private List<FriendRound> friendRounds;

    private FriendRoundAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_friend_round;
    }

    @Override
    protected void init() {
        back = (ImageView) findViewById(R.id.back);
        add = (ImageView) findViewById(R.id.add);
        imBack = (ImageView) findViewById(R.id.im_back);
        im = (ImageView) findViewById(R.id.im);
        lv = (ListView) findViewById(R.id.lv);
        name = (TextView) findViewById(R.id.name);

        chatTableDao = new ChatTableDaoImpl(this, AbChatApplication.getAccount().getChatTableDbName());
        friendRoundDao = new FriendRoundImpl(this);

        String imgPath = AbChatApplication.getAccount().getImgPath();
        if (!TextUtils.isEmpty(imgPath)) {
            Glide.with(this)
                    .load(imgPath)
                    .transform(new RoundedCorners(18))
                    .into(im);
        }
        name.setText(AbChatApplication.getAccount().getName());

        initData();

    }

    private void initData(){
        try {
            allOfId = chatTableDao.findAllOfId();
        } catch (Exception e) {
            e.printStackTrace();
        }

        allOfId.add(AbChatApplication.getAccount().getId());

        try {
            friendRounds = friendRoundDao.findByListIds(allOfId);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (friendRounds != null && friendRounds.size() > 0){
            if (adapter == null){
                adapter = new FriendRoundAdapter(this,new AccountDaoImpl(this),friendRounds);
                lv.setAdapter(adapter);
            }else {
                adapter.setFriendRounds(friendRounds);
            }
        }
    }

    @Override
    protected void initEvent() {
        back.setOnClickListener(this);
        imBack.setOnClickListener(this);
        add.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finishThis();
                break;
            case R.id.im_back:
                break;
            case R.id.add:
                migrateTo(EditFriendRoundActivity.class);
                break;
        }
    }
}